package com.yanqu.backendweb.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

/**
 * @author duli
 * @version 1.0
 * @date 2020/8/31 17:47
 * @Description
 */
public enum eErrorCode {
    /**
     * 基础错误
     */
    SUCCESS(0,"ok"),
    FIELD__X_NOT_FOUND(300,"%s 字段相关表不存在"),
    SERVER_NOT_EXISTS(301,"区服不存在"),
    SERVER_NULL(302,"没有区服参数"),
    SIGN_ERROR(400,"验签错误"),
    FORM_ERROR(401, "表单验证错误"),
    REQUEST_METHOD_NOT_ALLOWED(402, "不允许的请求方法"),
    METHOD_ARGUMENT_ERROR(403, "请求参数转换错误"),
    SYSTEM_ERROR(500,"系统错误"),
    
    ;
    private int code;

    /*通用后台查询*/
    private String msg;
    eErrorCode(int code, String msg) {
        this.code = code;
        this.msg=msg;
    }

    public  int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static String getMsg(int code) {
        for (eErrorCode errorCode : eErrorCode.values()) {
            if (errorCode.getCode() == code) {
                return errorCode.getMsg();
            }
        }
        return "无错误信息";
    }
    public String toJson() {
        return toJson(null);
    }

    public String toJson(String msg) {
        if (StringUtils.isBlank(msg)) {
            msg = this.msg;
        }
        return "{\n\t\"code\": " + code + ",\n\t\"msg\": \"" + msg + "\"\n}";
    }

    public JSONObject toJSONObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", this.getCode());
        jsonObject.put("msg", this.getMsg());
        return jsonObject;
    }

}
